package com.seth.myapplication.login;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.seth.myapplication.R;
import com.seth.myapplication.bean.User;
import com.seth.myapplication.infos.GetUser;
import com.seth.myapplication.service.NetWorkService;
import com.seth.myapplication.utils.SmsSend;
import com.seth.myapplication.utils.StringHelper;


/**
 * Created by Seth on 2017/10/18.
 */
public class RegistPage extends Fragment {

    private EditText usrPhone;
    private EditText proveNum;
    private EditText usrPass;
    private EditText usrRePass;
    private EditText usrNickname;
    private Button getProveNum;
    private Button register;
    private TextView checkPass;
    private User mUser;
    private SmsSend smsSend;

    String strPhone = "";
    String strProveNum = "";
    String strNickname = "";
    String strPassword = "";
    String strRePassword = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_regist, container, false);

        smsSend = new SmsSend(getActivity(), getProveNum);
        smsSend.InitSms();
        initView(view);

        initEvent();

        return view;
    }

    private void initView(View view) {
        usrPhone = (EditText) view.findViewById(R.id.phone_num);
        proveNum = (EditText) view.findViewById(R.id.prove_num);
        usrPass = (EditText) view.findViewById(R.id.usr_pass);
        usrRePass = (EditText) view.findViewById(R.id.re_usr_pass);
        usrNickname = (EditText) view.findViewById(R.id.nick_name);
        getProveNum = (Button) view.findViewById(R.id.msg_btn);
        register = (Button) view.findViewById(R.id.regist_btn);
        checkPass = (TextView) view.findViewById(R.id.password_check);
    }

    private void initEvent() {


        usrPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(StringHelper.checkCellphone(s.toString())){
                        getProveNum.setEnabled(true);
                        checkInput();
                    }else{
                        getProveNum.setEnabled(false);
                    }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        usrNickname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkInput();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        usrPass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkInput();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        usrRePass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkInput();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        proveNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkInput();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        usrRePass.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus){
                    strPassword = usrPass.getText().toString();
                    strRePassword = usrRePass.getText().toString();
                    if(strPassword.equals(strRePassword)){
                        checkPass.setVisibility(View.INVISIBLE);
                    }else{
                        checkPass.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        getProveNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                smsSend.sendMsg(strPhone);
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = proveNum.getText().toString().replaceAll("/s","");
                //if(smsSend.CheckInput(code)){
                if(true){
                    mUser = new User(strNickname,strPassword,strPhone);
                    GetUser.setmUser(mUser);
                    new NetWorkService(getActivity()).newRegist(mUser,getContext());
                }else{
                    Toast.makeText(getContext(), "验证码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void checkInput(){
        strPassword = usrPass.getText().toString();
        strRePassword = usrRePass.getText().toString();
        strNickname = usrNickname.getText().toString();
        strPhone = usrPhone.getText().toString();
        strProveNum = proveNum.getText().toString();
        if(strProveNum.length()==0||strPhone.length()==0||strNickname.length()==0||strPassword.length()==0||!strPassword.equals(strRePassword)){
            register.setEnabled(false);
        }else{
            register.setEnabled(true);
        }

    }

}
